(function() {

/**
 * @author Igor Lazarev
 */

WireIt.WebUIInputContainer = function(options, layer) {

WireIt.WebUIInputContainer.superclass.constructor.call(this, options, layer);
	
		var pencil = this.bodyEl.getElementsByClassName("inputEx-TypeField-EditButton")[0];
		var pencilPanel = pencil.getElementsByClassName("inputEx-TypeField-PropertiesPanel")[0];
		var oldValue = undefined;
		
		YAHOO.util.Event.addListener(this.bodyEl,"change",function(){
			oldValue =this.getValue();
		}, this, true); 
		
		YAHOO.util.Event.addListener(pencilPanel,"change",function(){
			var value = this.getValue();
			if ((oldValue) && (!value.list)) {
				value.input.inputParams.value = oldValue.input.inputParams.value;
				this.setValue(value);
			}
			else if ((oldValue) && (value.list)) {
				value.input.inputParams.value = YAHOO.lang.JSON.stringify(value.list);
				this.setValue(value);
			}
		}, this, true); 
	
}

YAHOO.extend(WireIt.WebUIInputContainer, WireIt.WebUIFormContainer, {
	
	setOptions: function(options){
		WireIt.WebUIInputContainer.superclass.setOptions.call(this,options);
		
		this.options.value = "";
		this.options.oldValue = "";
		
	},
	
	getConfig: function() {
	 var obj = WireIt.WebUIInputContainer.superclass.getConfig.call(this);
		
		obj.term = this.terminals[0].options.name;
		if(typeof(this.schema)!="undefined"){
			obj.schema = this.schema;
		}
		else{
			obj.schema = {};
		}
		if(typeof(obj.schema.title)=="undefined"){
			obj.schema.title = this.getValue().input.inputParams.name;
		}
		if(typeof(obj.schema.type)=="undefined"){
			obj.schema.type = this.getValue().input.type;
		}
		if(this.getValue().input.inputParams.value!=""){		
			obj.schema["default"] = this.getValue().input.inputParams.value;
		}
		obj.constant = this.bodyEl.getElementsByClassName("constantCheckBox")[0].childNodes[2].value;

     return obj;	
	},
	
	onMouseDown: function() { //if the container is selected
	
	  if(this.layer) {
	  			
		 if(this.layer.focusedContainer && this.layer.focusedContainer != this) {
	        this.layer.focusedContainer.removeFocus();
			this.options.oldValue = this.getValue();
		}
		 else if (!this.layer.focusedContainer) {
			this.options.oldValue = this.getValue();
		 }
		
		 this.setFocus();
	     this.layer.focusedContainer = this;
		 this.layer.focusContainer(this); //fire a 'container focused' event
	  }
	
	},
	
	onResize: function(event, args){
		WireIt.WebUIInputContainer.superclass.onResize.call(this,event, args);
		this.positionTerminals();
		this.redrawAllWires();
	},

	positionTerminals: function(){
		var width = WireIt.getIntStyle(this.el, "width");
		var term = this.terminals[0];
		 WireIt.sn(term.el, null, {position: "absolute", bottom: "-15px", left: (Math.floor(width/2)-15)+"px"});
	}, 
	
	removeFocus: function(){
		WireIt.WebUIInputContainer.superclass.removeFocus.call(this);		
		
		this.options.value = this.getValue();
			
		 //check type changes
		 
		 if((this.options.value)&&(this.options.oldValue)){
				
				if (this.options.value.input.type != this.options.oldValue.input.type){
					this.initType();
					this.options.oldValue = this.options.value;
				}
				else if (this.options.value.input.inputParams.name != this.options.oldValue.input.inputParams.name){
					this.initType();
					this.options.oldValue = this.options.value;
				}
		 } 
	},
	
	renderUploadLink: function(){ // file upload form

		
		var trgID = "target_iframe_"+randomString("qwertyuiopasdfghjklzxcvbnm1234567890",20);
		var myFrm = "form_"+randomString("qwertyuiopasdfghjklzxcvbnm1234567890",20);
		var txtFile0 = "file_form_"+randomString("qwertyuiopasdfghjklzxcvbnm1234567890",20);
		var submitID1 = "submit_button_"+randomString("qwertyuiopasdfghjklzxcvbnm1234567890",20);
		var uploadStatusDiv = "upload_status_"+randomString("qwertyuiopasdfghjklzxcvbnm1234567890",20);
		var progressBar = "progress_bar_"+randomString("qwertyuiopasdfghjklzxcvbnm1234567890",20);
		var iframeName = "iframe_name_"+randomString("qwertyuiopasdfghjklzxcvbnm1234567890",20);

		var formFieldId = this.bodyEl.childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].id;
	
		if(!formFieldId){
			formFieldId = this.bodyEl.childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[0].childNodes[0].childNodes[1].childNodes[0].childNodes[0].id;
		}
		
		this.formFieldId = formFieldId;
		//alert("form field: " + formFieldId);
				 
		 var html = '<iframe id="' + trgID + '" name="'+iframeName+'" height="0" width="0" frameborder="0" scrolling="yes"></iframe>'+              
			'<form id="' + myFrm + '" enctype="multipart/form-data" method="post" target="'+iframeName+
			'" action="uploader/" onsubmit="submitPostUsingAjax(\''+ trgID+'\' , \''+progressBar+'\' , \''+ formFieldId + '\');">'+
    			'<input type="file" size ="12" name="txtFile0" id="' + txtFile0 + '"></input><br>'+
    			'<input type="submit" id="' + submitID1 + '" name="submit" value="Upload"></input>'+
			'</form>'+
			'<table width="10" height="5" sellpadding="0" cellspacing="0" border="0"><tr><td></td></tr></table>'+
			'<div id="' + uploadStatusDiv + '" style="width:0px; height:0px; visibility:hidden;"></div>'+
			'<table id="' + progressBar + '"height="5px" width="0px" style="border:5px; width:0px; background-color: #3366CC; color:black; visibility:hidden; ">'+
			'<tr><td></td></tr>'+
			'</table>'+
			'<table width="10" height="5" sellpadding="0" cellspacing="0" border="0"><tr><td></td></tr></table>';
		//alert("html: " + html);
						 
		 var show ='<font color="#3366CC"><u>Upload file</u></font>';
		 var hide ='<font color="#3366CC"><u>Hide form</u></font>';
		 		 
		 this.uploadElement = inputEx.cn('div',null,{},html);
		 this.uploadLink = inputEx.cn('div',null,{},show);
		 this.uploadLink.style.cursor="pointer";
		this.uploadLink.setAttribute("class", "uploadLink");
		 this.hideLink = inputEx.cn('div',null,{},hide);
		 this.hideLink.style.cursor="pointer";
		this.hideLink.setAttribute("class", "hideLink");
						
		if(!this.uploadLink.parentNode){
			this.bodyEl.appendChild(this.uploadLink);
			this.bodyEl.appendChild(this.uploadElement);
			this.uploadElement.style.display = 'none';
			this.bodyEl.appendChild(this.hideLink);
			this.hideLink.style.display = 'none';
		};

		
		 this.uploadLink.onclick = function(){ //show file upload form
			
			this.uploadLink.style.display = 'none';
			this.uploadElement.style.display = 'block';
			this.hideLink.style.display = 'block';
			this.terminals[0].redrawAllWires();					
			
				this.hideLink.onclick = function(){
					this.hideLink.style.display = 'none';
					this.uploadElement.style.display = 'none';	
					this.uploadLink.style.display = 'block';
					this.terminals[0].redrawAllWires();

				}.bind(this);
				
		}.bind(this);
		
		var pencil = this.bodyEl.getElementsByClassName("inputEx-TypeField-EditButton")[0].getElementsByClassName("inputEx-TypeField-PropertiesPanel")[0].childNodes[0];
		//var pencil = this.bodyEl.childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[1].childNodes[0].childNodes[0];
		pencilPanelEl = $(pencil.id+'-field');
		
		//alert("pencil: "+pencil.id);
		
		YAHOO.util.Event.addListener(pencilPanelEl,"change",function(){
			if(this.uploadLink.parentNode){
				this.bodyEl.removeChild(this.uploadLink);
			}
			else if (this.uploadElement.parentNode) {
				this.bodyEl.removeChild(this.uploadElement);
				this.bodyEl.removeChild(this.hideLink);	
			}
			YAHOO.util.Event.removeListener(pencilPanelEl,"change");			
		}, this, true); 
	},
	
	initType: function(){
		
		if((this.terminals[0])&&this.getValue().input.type==this.terminals[0].options.ddConfig.type.split(":")[1]){ //compare types of field and terminal

			var sameType = true;
			var wires = this.wires;
			var connected = new Array;

			for (i=0;i<wires.length;i++){
				connected[i] = wires[i].getOtherTerminal(this.terminals[0]);
			}
		}else{
			if(this.getValue().input.type=="file"){
				this.renderUploadLink();
			}
		}
				
		this.removeAllTerminals();

		if (this.getValue().input.inputParams.name){
			this.addTerminal({xtype: "WireIt.util.WebUITerminalOutput", 
								"name": this.getValue().input.inputParams.name + ":"+this.getValue().input.type,
								"ddConfig":{"type":YAHOO.lang.JSON.stringify(this.getValue().input.type)}, 
									"direction": [0,1], 
									"offsetPosition": {"left": 86, "bottom": -15}});
			var value = this.getValue();
			this.setName(value.input.inputParams.name);
			this.positionTerminals();
		}
		else{
			var value = this.getValue();
			value.input.inputParams.name = "input";
			this.setValue(value);
			
			this.addTerminal({xtype: "WireIt.util.WebUITerminalOutput", 
								"name": "input"+":"+this.getValue().input.type,
								"ddConfig":{"type":YAHOO.lang.JSON.stringify(this.getValue().input.type)}, 
									"direction": [0,1], 
									"offsetPosition": {"left": 86, "bottom": -15}});
			this.setName(this.getValue().input.inputParams.name);
			this.positionTerminals();
		}
		this.dd.setTerminals(this.terminals);
	
		if(sameType){ //the type of the container has not changed
			for (i=0;i<wires.length;i++){
				var wire = {"src":{"moduleId":WireIt.indexOf(this, this.layer.containers),"terminal":this.terminals[0].options.name},
									"tgt":{"moduleId":WireIt.indexOf(connected[i].container, this.layer.containers),"terminal": connected[i].options.name}};
				this.layer.addWire(wire);
				this.terminals[0].redrawAllWires();			
				
			}
		}

	},	 
	
	initState: function(){

	this.setReady();
					
		if((!this.getValue().input.inputParams.value)||(this.getValue().input.inputParams.value=="[]")){
			if(this.getValue().input.type!="boolean"){			
				this.setUnready();
			}
		}
						
	},
	
	setName: function(title){
		this.ddHandle.removeChild(this.title);
		this.title = WireIt.cn('span', null, null, title);
		this.ddHandle.appendChild(this.title);

	}


	
})

})()
